package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.vo.PCountVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fenge
 */
@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {

    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;

    @Autowired
    private CustomerReturnListDao customerReturnListDao;

    @Override
    public void save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        customerReturnList.setUserId(userId);
        customerReturnListDao.save(customerReturnList);
        Gson gson = new Gson();
        List<CustomerReturnListGoods> customerReturnListGoods = gson.fromJson(customerReturnListGoodsStr,new TypeToken<List<CustomerReturnListGoods>>(){}.getType());
        customerReturnListGoods.forEach(customerReturnListGood->{
            customerReturnListGood.setCustomerReturnListId(customerReturnList.getCustomerReturnListId());
            customerReturnListGoodsDao.save(customerReturnListGood);
        });
    }

    @Override
    public Map<String, Object> list(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        Map<String, Object> map =new HashMap<>();
        List<CustomerReturnList> customerReturnLists = customerReturnListDao.list(returnNumber,customerId,state,sTime,eTime);
        map.put("rows",customerReturnLists);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer customerReturnListId) {
        Map<String, Object> map =new HashMap<>();
        List<CustomerReturnListGoods> customerReturnListGoods = customerReturnListGoodsDao.goodsList(customerReturnListId);
        map.put("rows",customerReturnListGoods);
        return map;
    }

    @Override
    public void delete(Integer customerReturnListId) {
        List<CustomerReturnListGoods> customerReturnListGoods = customerReturnListGoodsDao.goodsList(customerReturnListId);
        customerReturnListGoods.forEach(customerReturnListGood->{
            customerReturnListGoodsDao.delete(customerReturnListGood);
        });
        customerReturnListDao.delete(customerReturnListId);
    }

    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<PCountVo> pCountVos =customerReturnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        String s = gson.toJson(pCountVos);
        return s;
    }
}
